package com.chenjim.architecture.screen

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController

/**
 * 充值页面，豆包依据UI设计图生成
 */
@Composable
fun OnlineRechargeScreen(
    navController: NavHostController,
) {
    var selectedAmount by remember { mutableIntStateOf(50) }
    var selectedPaymentMethod by remember { mutableStateOf("微信支付") }

    Column(
        modifier = Modifier.padding(horizontal = 16.dp)
    ) {
        // 标题栏
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(WindowInsets.statusBars.asPaddingValues()),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = "←", fontSize = 24.sp
            )
            Text(
                text = "在线充值", fontSize = 20.sp, fontWeight = FontWeight.Bold
            )
        }

        // 充值金额部分
        Spacer(modifier = Modifier.height(16.dp))
        Text(
            text = "充值金额", fontSize = 16.sp, color = Color.Gray
        )
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = "￥ $selectedAmount", fontSize = 32.sp, modifier = Modifier.padding(vertical = 8.dp)
            )
        }
        Row(
            modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly
        ) {
            AmountButton(
                amount = 50, isSelected = selectedAmount == 50, onSelect = { selectedAmount = 50 })
            AmountButton(
                amount = 100, isSelected = selectedAmount == 100, onSelect = { selectedAmount = 100 })
            AmountButton(
                amount = 150, isSelected = selectedAmount == 150, onSelect = { selectedAmount = 150 })
        }
        Row(
            modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly
        ) {
            AmountButton(
                amount = 200, isSelected = selectedAmount == 200, onSelect = { selectedAmount = 200 })
            AmountButton(
                amount = 300, isSelected = selectedAmount == 300, onSelect = { selectedAmount = 300 })
            AmountButton(
                amount = 500, isSelected = selectedAmount == 500, onSelect = { selectedAmount = 500 })
        }

        // 充值方式部分
        Spacer(modifier = Modifier.height(16.dp))
        Text(
            text = "充值方式", fontSize = 16.sp, color = Color.Gray
        )
        PaymentMethodItem(
            method = "微信支付",
            isSelected = selectedPaymentMethod == "微信支付",
            onSelect = { selectedPaymentMethod = "微信支付" })
        PaymentMethodItem(
            method = "支付宝",
            isSelected = selectedPaymentMethod == "支付宝",
            onSelect = { selectedPaymentMethod = "支付宝" })

        // 立即充值按钮
        Spacer(modifier = Modifier.height(24.dp))
        Button(
            onClick = { /* 这里添加充值逻辑 */ },
            modifier = Modifier.fillMaxWidth(),
            shape = RoundedCornerShape(24.dp),
            colors = ButtonDefaults.buttonColors(
                containerColor = Color(0xFF008577)
            )
        ) {
            Text(
                text = "立即充值", fontSize = 18.sp, color = Color.White, fontWeight = FontWeight.Bold
            )
        }
    }
}

@Composable
fun AmountButton(
    amount: Int, isSelected: Boolean, onSelect: () -> Unit
) {
    Button(
        onClick = onSelect,
        modifier = Modifier
            .width(80.dp)
            .height(40.dp),
        shape = RoundedCornerShape(8.dp),
        colors = ButtonDefaults.buttonColors(
            containerColor = if (isSelected) Color(0xFF008577) else Color(0xFFF0F0F0)
        )
    ) {
        Text(
            text = "$amount", fontSize = 18.sp, color = if (isSelected) Color.White else Color.Black
        )
    }
}

@Composable
fun PaymentMethodItem(
    method: String, isSelected: Boolean, onSelect: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 8.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            when (method) {
                "微信支付" -> {
                    // 这里如果要添加图标可以使用 painterResource 加载资源
                    // Icon(painter = painterResource(id = R.drawable.wechat_icon), contentDescription = null)
                    Text(
                        text = method,
                        fontSize = 18.sp,
                        fontWeight = FontWeight.Bold,
                        color = if (isSelected) Color(0xFF008577) else Color.Black
                    )
                }

                "支付宝" -> {
                    // 同理添加支付宝图标
                    // Icon(painter = painterResource(id = R.drawable.alipay_icon), contentDescription = null)
                    Text(
                        text = method,
                        fontSize = 18.sp,
                        fontWeight = FontWeight.Bold,
                        color = if (isSelected) Color(0xFF008577) else Color.Black
                    )
                }
            }
        }
        Checkbox(
            checked = isSelected, onCheckedChange = { onSelect() }, colors = CheckboxDefaults.colors(
                checkedColor = Color(0xFF008577)
            )
        )
    }
}